library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5     ✓ purrr   0.3.4
## ✓ tibble  3.1.6     ✓ dplyr   1.0.7
## ✓ tidyr   1.1.4     ✓ stringr 1.4.0
## ✓ readr   2.0.1     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
hpi_df <- read_csv("data/hpi-tidy.csv")
## Rows: 151 Columns: 11
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Country, GovernanceRank, Region
## dbl (8): HPIRank, LifeExpectancy, Wellbeing, HappyLifeYears, Footprint, Happ...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
hpi_df
## # A tibble: 151 × 11
##    HPIRank Country     LifeExpectancy Wellbeing HappyLifeYears Footprint
##      <dbl> <chr>                <dbl>     <dbl>          <dbl>     <dbl>
##  1     109 Afghanistan           48.7      4.76           29.0     0.540
##  2      18 Albania               76.9      5.27           48.8     1.81 
##  3      26 Algeria               73.1      5.24           46.2     1.65 
##  4     127 Angola                51.1      4.21           28.2     0.891
##  5      17 Argentina             75.9      6.44           55.0     2.71 
##  6      53 Armenia               74.2      4.37           41.9     1.73 
##  7      76 Australia             81.9      7.41           65.5     6.68 
##  8      48 Austria               80.9      7.35           64.3     5.29 
##  9      80 Azerbaijan            70.7      4.22           39.1     1.97 
## 10     146 Bahrain               75.1      4.55           43.5     6.65 
## # … with 141 more rows, and 5 more variables: HappyPlanetIndex <dbl>,
## #   Population <dbl>, GDPcapita <dbl>, GovernanceRank <chr>, Region <chr>

Exericse 1. Make a map of a variable of your choosing. In coord_map(), use projection = “mercator”, which is also the default (we will see in a later exercise that this probably is not the best choice).

Hint: in ggplot2’s map_data() function, there is a built in map of the “world”.

Hint: You can read more about projections in Section 17.3.2 of Modern Data Science with R

world_df <- ggplot2::map_data("world")
world_full <- right_join(hpi_df, world_df, by = c("Country" = "region"))
ggplot(data = world_full,
            mapping = aes(x = long, y = lat,
                          group = group)) +
  geom_polygon(color = "black", aes(fill = LifeExpectancy)) +
  theme_void() +
  coord_map(projection = "mercator") +
  scale_fill_viridis_c()

Exercise 2. You may notice that the United States does not get coloured in your map. Examine this issue further and fix the map so that the United States is coloured.

hpi_df <- hpi_df %>% mutate(Country = fct_recode(Country, USA = "United States of America")) 

world_full <- right_join(hpi_df, world_df, by = c("Country" = "region"))
ggplot(data = world_full,
            mapping = aes(x = long, y = lat,
                          group = group)) +
  geom_polygon(color = "black", aes(fill = LifeExpectancy)) +
  theme_void() +
  coord_map(projection = "mercator") +
  scale_fill_viridis_c()

Exercise 3. You may have noticed that there are two horizontal stripes across your map. This is an issue that drove me nuts! Check out this submitted issue on ggplot2’s GitHub page for the reason for the error as well as a fix. Use it to fix your plot.

ggplot(data = world_full,
            mapping = aes(x = long, y = lat,
                          group = group)) +
  geom_polygon(color = "black", aes(fill = LifeExpectancy)) +
  theme_void() +
  coord_map(projection = "mercator", xlim = c(-180, 180)) +
  scale_fill_viridis_c()

Exercise 4. Read about Mercator projections in this blog post. What does this source say about the sizes of Greenland vs. Africa in a Mercator projection.

The size of Greenland is 550% too big as it should be fitting into Africa 14 times.

Exercise 5. Examine all of the different options for map projection with ?mapproject. Then, change the projection to “globular”. Change the projection again to “gilbert”. How does the relative size of Greenland to Africa change in the projections?

ggplot(data = world_full,
            mapping = aes(x = long, y = lat,
                          group = group)) +
  geom_polygon(color = "black", aes(fill = LifeExpectancy)) +
  theme_void() +
  coord_map(projection = "globular", xlim = c(-180, 180)) +
  scale_fill_viridis_c()

ggplot(data = world_full,
            mapping = aes(x = long, y = lat,
                          group = group)) +
  geom_polygon(color = "black", aes(fill = LifeExpectancy)) +
  theme_void() +
  coord_map(projection = "gilbert", xlim = c(-180, 180)) +
  scale_fill_viridis_c()

When looking at projection = “globular” we see that Greenland and Africa look to be their respective sizes with Greenland being much smaller than Africa. But when we look at projection = “gilbert’ we see that Greenland is now much bigger with respect to Africa. In”globular" Greenland likely could fit into Africa 14 times but it is unlikely to be able to do the same in “gilbert”.

library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
plot <- ggplot(data = world_full,
            mapping = aes(x = long, y = lat,
                          group = group, label = Country)) +
  geom_polygon(color = "black", aes(fill = LifeExpectancy)) +
  theme_void() +
  coord_map(projection = "gilbert", xlim = c(-180, 180)) +
  scale_fill_viridis_c()
ggplotly(plot, tooltip = "label")